﻿using System;
using System.Drawing;
using System.Linq;
using Barkod.Utility;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using Barkod.Properties;

namespace Barkod
{
    public class RibbonManager
    {
        public static int[]                
               Developers = new int[] { 1,2 };
                
        
        
        static int firstWhoAmI = -1;
        public static bool isDeveloper
        {
            get
            {
                if (firstWhoAmI == -1)
                    firstWhoAmI = AppSession.UserInfo.KullaniciId;

                return Developers.Contains(firstWhoAmI);
            }
        }
        
        public void Init()
        {
            RibbonControl.Pages.Clear();

            Image newIcon = Resources.miNew;
            Image listIcon = Resources.miTable;

            RibbonManager.Page page;
            RibbonManager.Group group;
            RibbonManager.Menu menu;
            RibbonManager.SubMenu subMenu;

            switch (AppSession.UserInfo.KullaniciId)
            {
                default:
                    page = AddPage(RibbonControl, "Ana Sayfa");
                    group = page.AddGroup("Temel İşlemler");                     
                    
                    
                    break;
            }
        }

        #region Ribbon Manager
        RibbonControl ribbonControl;

        public RibbonControl RibbonControl
        {
            get { return ribbonControl; }
            set { ribbonControl = value; }
        }

        public RibbonManager(RibbonControl ribbonControl)
        {
            this.ribbonControl = ribbonControl;
            this.ribbonControl.TransparentEditors =
            this.ribbonControl.Manager.TransparentEditors = true;
            Init();
        }

        public Page AddPage(RibbonControl Ribbon, string Caption)
        {
            RibbonPage ribbonPage = new RibbonPage(Caption);
            Page page = new Page(this, ribbonPage);
            if (Caption == "Ana Sayfa")
            {
                Ribbon.Pages.Insert(0, ribbonPage);
                Ribbon.SelectedPage = ribbonPage;
            }
            else
                Ribbon.Pages.Add(ribbonPage);

            return page;
        }

        public class Page
        {
            private RibbonManager parent;
            public RibbonManager Parent
            {
                get { return parent; }
                set { parent = value; }
            }

            private RibbonPage ribbonPage;

            public RibbonPage RibbonPage
            {
                get { return ribbonPage; }
                set { ribbonPage = value; }
            }
            public Page(RibbonManager parent, RibbonPage ribbonPage)
            {
                this.RibbonPage = ribbonPage;
                this.Parent = parent;
            }

            public Group AddGroup(string Caption)
            {
                RibbonPageGroup ribbonPageGroup = new RibbonPageGroup(Caption);
                RibbonPage.Groups.Add(ribbonPageGroup);
                ribbonPageGroup.ShowCaptionButton = false;
                return new Group(this, ribbonPageGroup);
            }
        }

        public class Group
        {
            private Page parent;
            public Page Parent
            {
                get { return parent; }
                set { parent = value; }
            }

            private RibbonPageGroup ribbonPageGroup;

            public RibbonPageGroup RibbonPageGroup
            {
                get { return ribbonPageGroup; }
                set { ribbonPageGroup = value; }
            }

            public Group(Page parent, RibbonPageGroup RibbonPageGroup)
            {
                this.RibbonPageGroup = RibbonPageGroup;
                this.Parent = parent;
            }

            public Menu AddMenu(string Caption, Bitmap icon)
            {
                BarButtonItem bbi = new BarButtonItem();
                bbi.Caption = Caption;
                bbi.RibbonStyle = DevExpress.XtraBars.Ribbon.RibbonItemStyles.Large;
                bbi.Glyph = icon;
                this.RibbonPageGroup.ItemLinks.Add(bbi);
                return new Menu(this, bbi);
            }

            public Type RelatedForm = null;
            object[] Params = null;
            public Menu AddMenu(string Caption, Bitmap icon, Type type, params object[] parameters)
            {
                RelatedForm = type;
                Params = parameters;
                Menu menu = AddMenu(Caption, icon);
                menu.BarButtonItem.ItemClick += delegate(object sender, ItemClickEventArgs e)

                { AppSession.MainForm.ShowMdiChildForm(type, parameters); };


                return new Menu(this, menu.BarButtonItem);
            }
        }

        public class Menu
        {
            private Group parent;
            public Group Parent
            {
                get { return parent; }
                set { parent = value; }
            }

            private BarButtonItem barButtonItem;
            public BarButtonItem BarButtonItem
            {
                get { return barButtonItem; }
                set { barButtonItem = value; }
            }

            public Menu(Group parent, BarButtonItem barButtonItem)
            {
                this.BarButtonItem = barButtonItem;
                this.Parent = parent;
            }

            public void AddCustomAction(Action Method)
            {
                this.BarButtonItem.ItemClick += delegate(object sender, ItemClickEventArgs e)
                {
                    Method();
                };
            }

            public SubMenu AddSubMenu(bool BeginGroup, string Caption, Image icon, Type type, params object[] parameters)
            {
                SubMenu sm = AddSubMenu(Caption, icon, type, parameters);

                foreach (BarItemLink bi in sm.BarButtonItem.Links)
                    if (bi.Item == sm.BarButtonItem)
                    {
                        bi.BeginGroup = true;
                        break;
                    }

                return sm;
            }

            public SubMenu AddSubMenu(string Caption, Image icon, Type type, params object[] parameters)
            {
                SubMenu subMenu = AddSubMenu(Caption, icon);
                subMenu.BarButtonItem.ItemClick += delegate(object sender, ItemClickEventArgs e)

                { AppSession.MainForm.ShowMdiChildForm(type, parameters); };

                return new SubMenu(this, subMenu.BarButtonItem);
            }

            public SubMenu AddSubMenu(string Caption, Image icon)
            {
                BarButtonItem.ActAsDropDown = true;
                BarButtonItem.ButtonStyle = DevExpress.XtraBars.BarButtonStyle.DropDown;
                PopupMenu popupMenu = null;
                if (BarButtonItem.DropDownControl == null)
                {
                    popupMenu = new PopupMenu(this.Parent.Parent.Parent.RibbonControl.Manager);
                    BarButtonItem.DropDownControl = popupMenu;
                }
                else
                {
                    popupMenu = (PopupMenu)BarButtonItem.DropDownControl;
                }

                BarButtonItem subMenu = new BarButtonItem();
                subMenu.Caption = Caption;
                subMenu.Glyph = icon;

                popupMenu.AddItem(subMenu);

                return new SubMenu(this, subMenu);
            }

            int[] allowedUserIds = null;
           
            private void CheckAllowed()
            {
                bool allowed = false;
                 
                if (allowedUserIds != null)
                    allowed |= allowedUserIds.Contains(AppSession.UserInfo.KullaniciId);
                 
                this.BarButtonItem.Visibility = allowed ? BarItemVisibility.Always : BarItemVisibility.Never;
                this.BarButtonItem.Links[0].Visible = allowed;
            }

            public void SetVisibleForUsers(params int[] UserIds)
            {
                allowedUserIds = UserIds;

                CheckAllowed();
            }

            public void Hide()
            {
                allowedUserIds = new int[] { };

                CheckAllowed();
            }

         

            public void BeginGroup()
            {
                foreach (BarItemLink bi in this.Parent.RibbonPageGroup.ItemLinks)
                {
                    if (bi.Item == this.BarButtonItem)
                    {
                        bi.BeginGroup = true;
                        return;
                    }
                }
            }
        }

        public class SubMenu
        {
            private Menu parent;
            public Menu Parent
            {
                get { return parent; }
                set { parent = value; }
            }

            private BarButtonItem barButtonItem;

            public BarButtonItem BarButtonItem
            {
                get { return barButtonItem; }
                set { barButtonItem = value; }
            }

            public SubMenu(Menu parent, BarButtonItem barButtonItem)
            {
                this.BarButtonItem = barButtonItem;
                this.Parent = parent;
            }

            public void AddCustomAction(Action Method)
            {
                this.BarButtonItem.ItemClick += delegate(object sender, ItemClickEventArgs e)
                {
                    Method();
                };
            }

            

            int[] allowedUserIds = null;
           
            public void SetVisibleForUsers(params int[] UserIds)
            {
                allowedUserIds = UserIds;

                CheckAllowed();
            }

            public void Hide()
            {
                allowedUserIds = new int[] { };

                CheckAllowed();
            }

            private void CheckAllowed()
            {
                bool allowed = false;
 

                if (allowedUserIds != null)
                {
                    allowed |= allowedUserIds.Contains(AppSession.UserInfo.KullaniciId);
                }

               
                if (!allowed)
                    this.BarButtonItem.Visibility = BarItemVisibility.Never;
            }

          
        }
        #endregion
    }
}